import os
import base64
import requests
from API_KEY import *
import sys
import kokoro
sys.path.append('D:\\BIANCHENG\\script\\exercise\\py\\tts')
from tts import text_to_sound
def image_to_base64(image_path):    #转成base64
    with open(image_path, "rb") as image_file:
        return base64.b64encode(image_file.read()).decode('utf-8')

# 设置请求参数
api_url = "https://ark.cn-beijing.volces.com/api/v3/chat/completions"
headers = {
    "Content-Type": "application/json",
    "Authorization": f"Bearer {YI_KEY}"
}

image_path = "image.png"
base64_image = image_to_base64(image_path)
question = "这是什么?用一句概括,不要超过20个字"
# 构建请求数据
data = {
    "model": "doubao-seed-1-6-flash-250828",
    "messages": [
        {
            "role": "user",
            "content": [
                {
                    "type": "image_url",
                    "image_url": {
                         "url": f"data:image/jpeg;base64,{base64_image}"
                    },
                },
                {"type": "text", "text":question},
            ],
        }
    ],
}

# 发送请求
response = requests.post(api_url, headers=headers, json=data)

# 检查请求是否成功
if response.status_code == 200:
    result = response.json()
    sentence = result["choices"][0]["message"]["content"]
    print(result["choices"][0]["message"]["content"])
    text_to_sound(sentence)
    
else:
    print(f"请求失败，状态码: {response.status_code}")
    print(f"错误信息: {response.text}")
